package fileProcessors.implementations;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class KorrelationMittelungSorted extends KorrelationMittelung {
	private ArrayList<TreeMap<String, Double>> dataMap;


	public void processFiles(File files[], File currentDirectory, File outputFile) {
		super.readData(files,outputFile);
		readDataMapped(files, outputFile);
		printResult(files);
	}

	protected void printResult(File[] files) {
		try {
			super.printStatistics(files);
			printData();

			outputWriter.close();
		} catch (IOException e) {

		}
	}

	private void readDataMapped(File[] files, File outputFile) {
		// gespeicherten Datensatz l�schen
		dataMap = new ArrayList<TreeMap<String, Double>>();
		for (int i = 0; i < 6; i++) {
			dataMap.add(new TreeMap<String, Double>());

		}

		try {
			outputWriter = new BufferedWriter(new FileWriter(outputFile));
		} catch (IOException e2) {
		}
		for (int i = 0; i < files.length; i++) {
			BufferedReader br;
			try {
				br = new BufferedReader(new FileReader(files[i]));
				String[] line = null;
				for (int j = 0; j < data.size(); j++) {
					line = br.readLine().split("\t", 100);
					// wenn hier nicht a...steht, ist die Datei ung�ltig
					if (!line[0].equals("a" + j)) {
						outputWriter.write("Parameter a" + j + " konnte nicht gelesen werden.");
						outputWriter.newLine();
					} else {
						String filename = files[i].getName();
						dataMap.get(j).put(filename, Double.parseDouble(line[1]));
					}
				}
				br.close();
			} catch (IOException e2) {
				try {
					outputWriter.write("WARNUNG: Datei" + files[i].getAbsolutePath() + " konnte nicht gelesen werden.");
					outputWriter.newLine();
				} catch (IOException e1) {
				}
			}
			try {
				outputWriter.write(files[i].getName() + " geladen.");
				outputWriter.newLine();
			} catch (IOException e) {

			}
		}
	}

	private void printData() throws IOException {
		for (int i = 0; i < dataMap.size(); i++) {
			outputWriter.newLine();
			outputWriter.write("a" + i);
			outputWriter.newLine();

			TreeMap<String, Double> sortedMap = new TreeMap<String, Double>(new ValueComparator(dataMap.get(i)));
			
			sortedMap.putAll(dataMap.get(i));
			for (Entry<?, ?> entry : sortedMap.entrySet()) {
				Double value = (Double) entry.getValue();
				String file=(String) entry.getKey();
				outputWriter.write(value + "\t"+file.substring(
						file.length() - 13));
				outputWriter.newLine();
			}
		}
	}
	
	@Override
	public String getInformation() {
		return 	"Mittelt �ber _result.txt Dateien (ausgegeben vom Maple Fit-Programm vom 05.06.2012) und " +
				"gibt von allen ausgew�hlten Dateien von a0 bis a5 Mittelwert und Standardabweichung aus. "
						+ "(Dezimaltrennzeichen: Punkt). Die Werte sind in aufsteigender Reihenfolge sortiert.";
	}

	@Override
	public String getDate() {
		return "23.08.2012";
	}
}

class ValueComparator implements Comparator<Object> {

	Map<?, ?> base;

	public ValueComparator(Map<?, ?> base) {
		this.base = base;
	}

	public int compare(Object a, Object b) {
		return ((Double)base.get(a)).compareTo((Double)base.get(b));
	}
}
